﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace NetFrameworkExtensions.DataLayer.EF4
{
  public abstract class IdentifiedData<TIdentified, TIdentifier> :
    IIdentifiedData<TIdentified, TIdentifier>
    where TIdentified : IdentifiedData<TIdentified, TIdentifier>
    where TIdentifier : IEquatable<TIdentifier>
  {

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Column("Id")]
    public virtual TIdentifier Identifier { get; set; }

    [NotMapped]
    object IIdentifiedData.Identifier
    {
      get { return Identifier; }
      set { Identifier = (TIdentifier)value; }
    }

    [NotMapped]
    Type IIdentifiedData.IdentifiedType
    {
      get { return typeof(TIdentified); }
    }

    [NotMapped]
    Type IIdentifiedData.IdentifierType
    {
      get { return typeof(TIdentifier); }
    }
  }

  public abstract class IdentityIdentifiedData<TIdentified, TIdentifier> :
    IIdentifiedData<TIdentified, TIdentifier>
    where TIdentified : IdentityIdentifiedData<TIdentified, TIdentifier>
    where TIdentifier : IEquatable<TIdentifier>
  {

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("Id")]
    public virtual TIdentifier Identifier { get; set; }

    [NotMapped]
    object IIdentifiedData.Identifier
    {
      get { return Identifier; }
      set { Identifier = (TIdentifier)value; }
    }

    [NotMapped]
    Type IIdentifiedData.IdentifiedType
    {
      get { return typeof(TIdentified); }
    }

    [NotMapped]
    Type IIdentifiedData.IdentifierType
    {
      get { return typeof(TIdentifier); }
    }
  }
}
